<?php


namespace app\front\controller;



use app\exception\HttpExceptions;
use app\model\Manager;
use think\facade\Db;
use think\Request;
use app\model\Coin as CoinModel;
class Coin extends FrontAuth
{

    public function student (Request $request) {
       $sid = $request->post('sid');
       $count = $request->post('count');
       if(!is_numeric($sid) || !is_numeric($count)) throw  new HttpExceptions('非法参数');
       $user = Db::name('user')->find($this->uid);
       if($user['coin'] < $count) throw new HttpExceptions('希望币不足！！', 400);
       $user['coin'] = $user['coin'] - $count;
       $user['used_coin'] = $user['used_coin'] + $count;
       $student= Db::name('student')->find($sid);
       $student['coin'] = $student['coin'] + $count;
       Db::name('student')->save($student);
       Db::name('user')->save($user);
       return $this->result();
    }
    public function origanziation (Request $request) {
       $oid = $request->post('oid');
       $count = $request->post('count');
       if(!is_numeric($oid) || !is_numeric($count)) throw  new HttpExceptions('非法参数');
       $user = Db::name('user')->find($this->uid);
       if($user['coin'] < $count) throw new HttpExceptions('希望币不足！！', 400);
       $user['coin'] = $user['coin'] - $count;
       $user['used_coin'] = $user['used_coin'] + $count;
       $origanziation= Db::name('origanziation')->find($oid);
       $origanziation['coin'] = $origanziation['coin'] + $count;
       Db::name('origanziation')->save($origanziation);
       Db::name('user')->save($user);
       return $this->result();
    }
    public function take(Request $request) {
      $code = $request -> get('code');
      if(!isset($code)) throw new HttpExceptions('非法参数');
      $res = CoinModel::take($code,$this->uid);
      if($res) return $this-> result();
      throw new HttpExceptions('无效激活码',400);
    }
    public function  add() {
        $share = date('Y-m-d', time());
        $user = Db::name('user')-> find($this->uid);
        if($user['share_date'] == $share) {
          throw new HttpExceptions('今日分享已领币',405);
        } else {
            CoinModel::addCoin($this->uid,$share);
            return $this->result();
        }


    }

    public function useCoin() {
        $data = Db::name('user')->find($this->uid);
        $data = array_diff_key($data,['id' => 'xxx']);
        $data = array_diff_key($data,['openid' => 'xxx']);
        $data = array_diff_key($data,['uname' => 'xxx']);
        $data = array_diff_key($data,['is_notice' => 'xxx']);
        return $this->result($data);
    }

    public function advise(Request $request) {
        $advise = $request -> post('advise');
        $uid = $this->uid;
        if(!isset($advise) || $advise == '') {
            throw  new HttpExceptions('非法传参',400);
        }

        $create_time = date('Y-m-d h:i:s', time());
        Db::name('advise')->save(compact('uid','advise','create_time'));
        $manger = Manager::find(1);
        $manger -> advise = $manger->advise + 1;
        $manger->save();
        return $this->result();
    }

}